1   package org.apache.lucene.facet.taxonomy;
2   
3   import java.util.Arrays;
4   
5   import org.apache.lucene.facet.FacetField;
6   import org.apache.lucene.facet.FacetTestCase;
7   import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField;
8   import org.apache.lucene.util.BytesRef;
9   import org.apache.lucene.util.TestUtil;
10  import org.junit.Test;
11  
12  /*
13   * Licensed to the Apache Software Foundation (ASF) under one or more
14   * contributor license agreements.  See the NOTICE file distributed with
15   * this work for additional information regarding copyright ownership.
16   * The ASF licenses this file to You under the Apache License, Version 2.0
17   * (the "License"); you may not use this file except in compliance with
18   * the License.  You may obtain a copy of the License at
19   *
20   *     http://www.apache.org/licenses/LICENSE-2.0
21   *
22   * Unless required by applicable law or agreed to in writing, software
23   * distributed under the License is distributed on an "AS IS" BASIS,
24   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25   * See the License for the specific language governing permissions and
26   * limitations under the License.
27   */
28  
29  public class TestFacetLabel extends FacetTestCase {
30    
31    @Test 
32    public void testBasic() {
33      assertEquals(0, new FacetLabel().length);
34      assertEquals(1, new FacetLabel("hello").length);
35      assertEquals(2, new FacetLabel("hello", "world").length);
36    }
37    
38    @Test 
39    public void testToString() {
40      // When the category is empty, we expect an empty string
41      assertEquals("FacetLabel: []", new FacetLabel().toString());
42      // one category
43      assertEquals("FacetLabel: [hello]", new FacetLabel("hello").toString());
44      // more than one category
45      assertEquals("FacetLabel: [hello, world]", new FacetLabel("hello", "world").toString());
46    }
47  
48    @Test 
49    public void testGetComponent() {
50      String[] components = new String[atLeast(10)];
51      for (int i = 0; i < components.length; i++) {
52        components[i] = Integer.toString(i);
53      }
54      FacetLabel cp = new FacetLabel(components);
55      for (int i = 0; i < components.length; i++) {
56        assertEquals(i, Integer.parseInt(cp.components[i]));
57      }
58    }
59  
60    @Test
61    public void testDefaultConstructor() {
62      // test that the default constructor (no parameters) currently
63      // defaults to creating an object with a 0 initial capacity.
64      // If we change this default later, we also need to change this
65      // test.
66      FacetLabel p = new FacetLabel();
67      assertEquals(0, p.length);
68      assertEquals("FacetLabel: []", p.toString());
69    }
70    
71    @Test 
72    public void testSubPath() {
73      final FacetLabel p = new FacetLabel("hi", "there", "man");
74      assertEquals(p.length, 3);
75      
76      FacetLabel p1 = p.subpath(2);
77      assertEquals(2, p1.length);
78      assertEquals("FacetLabel: [hi, there]", p1.toString());
79  
80      p1 = p.subpath(1);
81      assertEquals(1, p1.length);
82      assertEquals("FacetLabel: [hi]", p1.toString());
83  
84      p1 = p.subpath(0);
85      assertEquals(0, p1.length);
86      assertEquals("FacetLabel: []", p1.toString());
87  
88      // with all the following lengths, the prefix should be the whole path 
89      int[] lengths = { 3, -1, 4 };
90      for (int i = 0; i < lengths.length; i++) {
91        p1 = p.subpath(lengths[i]);
92        assertEquals(3, p1.length);
93        assertEquals("FacetLabel: [hi, there, man]", p1.toString());
94        assertEquals(p, p1);
95      }
96    }
97  
98    @Test 
99    public void testEquals() {
100     assertEquals(new FacetLabel(), new FacetLabel());
101     assertFalse(new FacetLabel().equals(new FacetLabel("hi")));
102     assertFalse(new FacetLabel().equals(Integer.valueOf(3)));
103     assertEquals(new FacetLabel("hello", "world"), new FacetLabel("hello", "world"));    
104   }
105   
106   @Test 
107   public void testHashCode() {
108     assertEquals(new FacetLabel().hashCode(), new FacetLabel().hashCode());
109     assertFalse(new FacetLabel().hashCode() == new FacetLabel("hi").hashCode());
110     assertEquals(new FacetLabel("hello", "world").hashCode(), new FacetLabel("hello", "world").hashCode());
111   }
112   
113   @Test 
114   public void testLongHashCode() {
115     assertEquals(new FacetLabel().longHashCode(), new FacetLabel().longHashCode());
116     assertFalse(new FacetLabel().longHashCode() == new FacetLabel("hi").longHashCode());
117     assertEquals(new FacetLabel("hello", "world").longHashCode(), new FacetLabel("hello", "world").longHashCode());
118   }
119   
120   @Test 
121   public void testArrayConstructor() {
122     FacetLabel p = new FacetLabel("hello", "world", "yo");
123     assertEquals(3, p.length);
124     assertEquals("FacetLabel: [hello, world, yo]", p.toString());
125   }
126   
127   @Test 
128   public void testCompareTo() {
129     FacetLabel p = new FacetLabel("a", "b", "c", "d");
130     FacetLabel pother = new FacetLabel("a", "b", "c", "d");
131     assertEquals(0, pother.compareTo(p));
132     assertEquals(0, p.compareTo(pother));
133     pother = new FacetLabel();
134     assertTrue(pother.compareTo(p) < 0);
135     assertTrue(p.compareTo(pother) > 0);
136     pother = new FacetLabel("a", "b_", "c", "d");
137     assertTrue(pother.compareTo(p) > 0);
138     assertTrue(p.compareTo(pother) < 0);
139     pother = new FacetLabel("a", "b", "c");
140     assertTrue(pother.compareTo(p) < 0);
141     assertTrue(p.compareTo(pother) > 0);
142     pother = new FacetLabel("a", "b", "c", "e");
143     assertTrue(pother.compareTo(p) > 0);
144     assertTrue(p.compareTo(pother) < 0);
145   }
146 
147   @Test
148   public void testEmptyNullComponents() throws Exception {
149     // LUCENE-4724: CategoryPath should not allow empty or null components
150     String[][] components_tests = new String[][] {
151       new String[] { "", "test" }, // empty in the beginning
152       new String[] { "test", "" }, // empty in the end
153       new String[] { "test", "", "foo" }, // empty in the middle
154       new String[] { null, "test" }, // null at the beginning
155       new String[] { "test", null }, // null in the end
156       new String[] { "test", null, "foo" }, // null in the middle
157     };
158 
159     for (String[] components : components_tests) {
160       try {
161         assertNotNull(new FacetLabel(components));
162         fail("empty or null components should not be allowed: " + Arrays.toString(components));
163       } catch (IllegalArgumentException e) {
164         // expected
165       }
166       try {
167         new FacetField("dim", components);
168         fail("empty or null components should not be allowed: " + Arrays.toString(components));
169       } catch (IllegalArgumentException e) {
170         // expected
171       }
172       try {
173         new AssociationFacetField(new BytesRef(), "dim", components);
174         fail("empty or null components should not be allowed: " + Arrays.toString(components));
175       } catch (IllegalArgumentException e) {
176         // expected
177       }
178       try {
179         new IntAssociationFacetField(17, "dim", components);
180         fail("empty or null components should not be allowed: " + Arrays.toString(components));
181       } catch (IllegalArgumentException e) {
182         // expected
183       }
184       try {
185         new FloatAssociationFacetField(17.0f, "dim", components);
186         fail("empty or null components should not be allowed: " + Arrays.toString(components));
187       } catch (IllegalArgumentException e) {
188         // expected
189       }
190     }
191     try {
192       new FacetField(null, new String[] {"abc"});
193       fail("empty or null components should not be allowed");
194     } catch (IllegalArgumentException e) {
195       // expected
196     }
197     try {
198       new FacetField("", new String[] {"abc"});
199       fail("empty or null components should not be allowed");
200     } catch (IllegalArgumentException e) {
201       // expected
202     }
203     try {
204       new IntAssociationFacetField(17, null, new String[] {"abc"});
205       fail("empty or null components should not be allowed");
206     } catch (IllegalArgumentException e) {
207       // expected
208     }
209     try {
210       new IntAssociationFacetField(17, "", new String[] {"abc"});
211       fail("empty or null components should not be allowed");
212     } catch (IllegalArgumentException e) {
213       // expected
214     }
215     try {
216       new FloatAssociationFacetField(17.0f, null, new String[] {"abc"});
217       fail("empty or null components should not be allowed");
218     } catch (IllegalArgumentException e) {
219       // expected
220     }
221     try {
222       new FloatAssociationFacetField(17.0f, "", new String[] {"abc"});
223       fail("empty or null components should not be allowed");
224     } catch (IllegalArgumentException e) {
225       // expected
226     }
227     try {
228       new AssociationFacetField(new BytesRef(), null, new String[] {"abc"});
229       fail("empty or null components should not be allowed");
230     } catch (IllegalArgumentException e) {
231       // expected
232     }
233     try {
234       new AssociationFacetField(new BytesRef(), "", new String[] {"abc"});
235       fail("empty or null components should not be allowed");
236     } catch (IllegalArgumentException e) {
237       // expected
238     }
239     try {
240       new SortedSetDocValuesFacetField(null, "abc");
241       fail("empty or null components should not be allowed");
242     } catch (IllegalArgumentException e) {
243       // expected
244     }
245     try {
246       new SortedSetDocValuesFacetField("", "abc");
247       fail("empty or null components should not be allowed");
248     } catch (IllegalArgumentException e) {
249       // expected
250     }
251     try {
252       new SortedSetDocValuesFacetField("dim", null);
253       fail("empty or null components should not be allowed");
254     } catch (IllegalArgumentException e) {
255       // expected
256     }
257     try {
258       new SortedSetDocValuesFacetField("dim", "");
259       fail("empty or null components should not be allowed");
260     } catch (IllegalArgumentException e) {
261       // expected
262     }
263   }
264 
265   @Test
266   public void testLongPath() throws Exception {
267     String bigComp = null;
268     while (true) {
269       int len = FacetLabel.MAX_CATEGORY_PATH_LENGTH;
270       bigComp = TestUtil.randomSimpleString(random(), len, len);
271       if (bigComp.indexOf('\u001f') != -1) {
272         continue;
273       }
274       break;
275     }
276 
277     try {
278       assertNotNull(new FacetLabel("dim", bigComp));
279       fail("long paths should not be allowed; len=" + bigComp.length());
280     } catch (IllegalArgumentException e) {
281       // expected
282     }
283   }
284 }